package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_level;
import com.wisemoresystem.seal.model.t_bank_verify;
import com.wisemoresystem.seal.model.t_location;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class VerifyService extends BaseService{
	
	@Autowired
	private BankService bService;
	@Autowired
	private BankSealService sbService;

	public List<Map<String, Object>> selectVerify(Page page){
		String ids = bService.selectUbank2();
		String sql = "select t.* from t_bank_verify t "
				+ " where t.status=0 and t.flag_type='机构审核' and t.flag is null and t.submit_user_id!=? "
				+ " and t.before_bank_id in(@ids) order by t.created desc ";
		
		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentUserId());
		sql = sql.replace("@ids", ids);
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		for (Map<String, Object> map : list) {
			String flag_note = map.get("flag_note")+"";
			if("机构撤销审核".equals(flag_note)){
				String before = map.get("before_v")+"";
				map = turnToMap(map,before);
			}else{
				String after = map.get("after_v")+"";
				map = turnToMap(map,after);
			}
		}
		return list;
	}
	
	public List<Map<String, Object>> selectUserVerify(String srh_name,Page page){
		String ids = bService.selectUbank2();
		String sql = "select t.* from t_bank_verify t "
				+ " where t.status=0 and t.flag_type='人员审核' and t.flag is null and t.before_v like ? and t.submit_user_id!=? "
				+ " and t.before_bank_id in("+ids+") order by t.created desc ";
		List<String> params = new ArrayList<String>();
		if(ChkTools.isNull(srh_name)){
			sql=sql.replace("and t.before_v like ? ","");
		}else{
			
			params.add("%"+"\"uname\":\""+srh_name+"%");
		}
		params.add(WebTools.getCurrentUserId());
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		for (Map<String, Object> map : list) {
			Map<String, Object> maps = new  HashMap<String,Object>();
			String before = map.get("before_v")+"";
			maps = JsonTools.jsonStrToMap(before);
			map.put("u_name", maps.get("uname"));
			map.put("u_code", maps.get("code"));
			t_bank bank = super.findObjById(maps.get("bank_id")+"", t_bank.class);
			if(ChkTools.isNotNull(bank)){
				map.put("u_bank", bank.getName());
			}
		}
		return list;
	}
	
	public Map<String,Object> turnToMap(Map<String,Object> map,String obj){
		Map<String,Object> m = JsonTools.jsonStrToMap(obj);
		map.put("code", m.get("code"));
		map.put("name", m.get("name"));
		map.put("type", m.get("type"));
		map.put("city_code", m.get("city_code"));
		map.put("note", m.get("note"));
		
		map.put("level_id", m.get("level_id"));
		t_bank_level level = super.findObjById(m.get("level_id")+"", t_bank_level.class);
		if(ChkTools.isNotNull(level))
			map.put("level_name", level.getName());
		
		map.put("area_id", m.get("area_id"));
		t_location location = super.findObjById(m.get("area_id")+"", t_location.class);
		if(ChkTools.isNotNull(location))
			map.put("area", location.getName()+"("+location.getCode()+")");
		
		map.put("super_id", m.get("super_id"));
		t_bank bank = super.findObjById(m.get("super_id")+"", t_bank.class);
		if(ChkTools.isNotNull(bank))
			map.put("super_name", bank.getName());
		map.put("bank_user_id", m.get("user_id"));
		return map;
	}

	@Transactional
	public String muli_choose(String check_ids, String type) {
		String[] ids = check_ids.split(",");
		for (String verify_id : ids) {
			t_bank_verify ver = super.findObjById(verify_id, t_bank_verify.class);
			if("yes".equals(type)){
				ver.setFlag("通过");
			}else{
				ver.setFlag("不通过");
			}
			ver.setUser_id(WebTools.getCurrentUserId());
			super.updateObj(ver);
			
			if(ver.getFlag().equals("通过")){
				Map<String,Object> before = new HashMap<String, Object>();
				before = turnToMap(before,ver.getBefore_v());
				t_bank bank = bService.selectBankByCode(before.get("code")+"");
				if(ver.getFlag_note().equals("机构撤销审核")){
					super.delObj(bank);
				}else if(ver.getFlag_note().equals("机构修改审核")){
					Map<String,Object> after = new HashMap<String, Object>();
					after = turnToMap(after,ver.getAfter_v());
					bank.setCode(after.get("code")+"");
					bank.setName(after.get("name")+"");
					bank.setCity_code(after.get("city_code")+"");
					bank.setLevel_id(after.get("level_id")+"");
					bank.setArea_id(after.get("area_id")+"");
					bank.setType(after.get("type")+"");
					bank.setFlag("启用");
					bank.setNote(after.get("note")+"");
					bank.setSuper_id(after.get("super_id")+"");
					super.updateObj(bank);
					
					t_bank_level lev = super.findObjById(before.get("level_id")+"", t_bank_level.class);
					t_bank_level lv = super.findObjById(after.get("level_id")+"", t_bank_level.class);
					
					bService.updateLowerBank(bank.get_id(),lev.getSort()-lv.getSort());
				}else{
					Map<String,Object> after = new HashMap<String, Object>();
					after = turnToMap(after,ver.getAfter_v());
					bank = new t_bank();
					bank.setCode(after.get("code")+"");
					bank.setName(after.get("name")+"");
					bank.setCity_code(after.get("city_code")+"");
					bank.setLevel_id(after.get("level_id")+"");
					bank.setArea_id(after.get("area_id")+"");
					bank.setType(after.get("type")+"");
					bank.setFlag("启用");
					bank.setNote(after.get("note")+"");
					bank.setUser_id(after.get("bank_user_id")+"");
					bank.setSuper_id(after.get("super_id")+"");
					super.saveObj(bank);
				}
				sbService.checkSealBankCode(bank.get_id());
			}else{
				Map<String,Object> before = new HashMap<String, Object>();
				before = turnToMap(before,ver.getBefore_v());
				t_bank bank = bService.selectBankByCode(before.get("code")+"");
				if(ChkTools.isNotNull(bank)){
					bank.setFlag("启用");
					super.updateObj(bank);
				}
			}
		}
		return "success";
	}
}
